package com.emm.yixun.poss.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.emm.yixun.common.response.ActionResponse;
import com.emm.yixun.common.utils.WebUtils;
import com.emm.yixun.poss.domain.TmAdmin;
import com.emm.yixun.poss.domain.TmMerchant;
import com.emm.yixun.poss.service.ITmMerchantService;
import com.emm.yixun.poss.vo.LoginVo;
import com.emm.yixun.poss.vo.MerchantVo;

@RestController
@RequestMapping("/merchant/")
public class MerchantController {
	@Autowired
	private ITmMerchantService iTmMerchantService;
	//商户列表
	@RequestMapping("list")
    public ActionResponse<Page<TmMerchant>> list(Integer pageNo,Integer pageSize){
		  ActionResponse<Page<TmMerchant>> response=new ActionResponse<Page<TmMerchant>>();
		  Page<TmMerchant> page=new Page<TmMerchant>(pageNo,pageSize);
		  EntityWrapper<TmMerchant> entityWrapper=new EntityWrapper<TmMerchant>();
		  entityWrapper.orderBy("create_time", false);
		  Page<TmMerchant> resultPage=iTmMerchantService.selectPage(page, entityWrapper);
		  response.success(resultPage);
		  return response;
    }
	//新增商户
	@RequestMapping("add")
    public ActionResponse<Boolean> add(MerchantVo merchantVo){
		ActionResponse<Boolean> ar=new ActionResponse<Boolean>();
		TmAdmin currentUser=WebUtils.getCurrentUser();
		if(null==currentUser){
			ar.failMessage("用户未登陆");
			return ar;
		}
		merchantVo.setCreateUserId(currentUser.getId());
		merchantVo.setMerchantCode(UUID.randomUUID().toString());
		try {
			iTmMerchantService.addMerchant(merchantVo);
			ar.success(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ar.failMessage("添加商户出错");;
		}
		
		return ar;
    }
	@RequestMapping("correctParameter")
	public ActionResponse<Boolean>  correctParameter(String merchantId,String key,String values,String sort){
		ActionResponse<Boolean> ar=new ActionResponse<Boolean>();
		try {
			iTmMerchantService.correctParameter(merchantId, key, values, sort);
			ar.success(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			ar.failMessage("初始化客户业务参("+key+")数错误");
		}
		return ar;
	}
}
